package cn.tomoki.myapplication;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;

import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;
import com.huawei.hms.ml.scan.HmsScanAnalyzerOptions;

import java.lang.ref.WeakReference;


//主界面
public class OutUserActivity extends AppCompatActivity {

    //动态申请权限标识
    public static final int CAMERA_REQ_CODE = 111;
    public static final int DECODE = 1;
    private static final int REQUEST_CODE_SCAN_ONE = 0X01;

    private Button buttonTwo;
    private Button buttonOne;
    private Button buttonThree;
    private ImageButton buttonPersonal;
    private ImageButton buttonFeedback;
    public static OutUserActivity instance;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.out_user);

        instance=this;

        buttonTwo = findViewById(R.id.outButtonTwo);
        buttonOne = findViewById(R.id.outButtonOne);
        buttonThree = findViewById(R.id.outButtonThree);
        buttonPersonal = findViewById(R.id.headPortrait);
        buttonFeedback = findViewById(R.id.outFeedback);
        setListeners();


    }

    private void setListeners() {
        OnClick onClick = new OnClick();
        buttonTwo.setOnClickListener(onClick);
//        buttonOne.setOnClickListener(onClick);
        buttonThree.setOnClickListener(onClick);
        buttonPersonal.setOnClickListener(onClick);
        buttonFeedback.setOnClickListener(onClick);
    }


    private class OnClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent intent = null;
            switch (v.getId()){
                //跳转到查询预约界面
                case R.id.outButtonTwo:
                    intent = new Intent(OutUserActivity.this,AppointmentActivity.class);
                    break;
//                case R.id.outButtonOne:
//                    intent = new Intent(MainActivity.this,.class);
//                    break;
                //跳转到出库历史
                case R.id.outButtonThree:
                    intent = new Intent(OutUserActivity.this,OutHistoryActivity.class);
                    break;
                //跳转到个人信息
                case R.id.headPortrait:
                    intent = new Intent(OutUserActivity.this,PersonalInformationActivity.class);
                    break;
                //跳转到反馈页面
                case R.id.outFeedback:
                    intent = new Intent(OutUserActivity.this,FeedbackActivity.class);
                    break;

            }
            startActivity(intent);
        }
    }

    //权限请求
    public void loadScanKitBtnClick(View view) {
        requestPermission(CAMERA_REQ_CODE, DECODE);
    }

    //编辑请求权限
    private void requestPermission(int requestCode, int mode) {
        ActivityCompat.requestPermissions(
                this,
                new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
                requestCode);
    }
    //权限申请返回
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (permissions == null || grantResults == null) {
            return;
        }

        if (grantResults.length < 2 || grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        if (requestCode == CAMERA_REQ_CODE) {
            //启动扫描Acticity
            ScanUtil.startScan(this, REQUEST_CODE_SCAN_ONE, new HmsScanAnalyzerOptions.Creator().create());
        }
    }
    //Activity回调
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK || data == null) {
            return;
        }
        if (requestCode == REQUEST_CODE_SCAN_ONE) {
            HmsScan obj = data.getParcelableExtra(ScanUtil.RESULT);
            if (obj != null) {
                Toast.makeText(this,obj.originalValue,Toast.LENGTH_SHORT).show();
            }
        }
    }
}